{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-f8c76f601ce6>, line 23)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-f8c76f601ce6>\"\u001b[1;36m, line \u001b[1;32m23\u001b[0m\n\u001b[1;33m    with tf.name_scope('loss')\u001b[0m\n\u001b[1;37m                              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#载入数据集\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "\n",
    "#定义每批次的大小\n",
    "batch_size=100\n",
    "#计算一共有多少批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "#命名空间\n",
    "with tf.name_scope('input'):\n",
    "    #定义变量\n",
    "    x = tf.placeholder(tf.float32,[None,784],name=\"x-input\")\n",
    "    y = tf.placeholder(tf.float32,[None,10],name=\"y-input\")\n",
    "\n",
    "with tf.name_scope('layer'):\n",
    "    #创建神经网络\n",
    "    with tf.name_scope('params'):\n",
    "        W = tf.Variable(tf.zeros([784,10]))\n",
    "        b = tf.Variable(tf.zeros([10]))\n",
    "    with tf.name_scope('softmax'):\n",
    "        predict = tf.nn.softmax(tf.matmul(x,W)+b)\n",
    "# predict = tf.nn.sigmoid(tf.matmul(x,W)+b)\n",
    "with tf.name_scope('loss'):\n",
    "    #代价函数\n",
    "    #loss = tf.reduce_mean(tf.square(y-predict))\n",
    "    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predict))\n",
    "with tf.name_scope('train'):\n",
    "    #梯度下降\n",
    "    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "with tf.name_scope('init'):\n",
    "    #初始化变量\n",
    "    init = tf.global_variables_initializer()\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        #验证结果\n",
    "        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(predict,1))\n",
    "    with tf.name_scope('accuracy'):\n",
    "        #正确率\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    #写入日志\n",
    "    writer = tf.summary.FileWriter('logs/',sess.graph)\n",
    "    for epoch in range(1):\n",
    "        for batch in range(n_batch):\n",
    "            batch_x,batch_y = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_x,y:batch_y})\n",
    "        \n",
    "        #计算正确率\n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print('iter: '+str(epoch)+',Testing Accuracy '+ str(acc))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
